using System;
using System.Linq;
using System.Windows.Input;
using Crm2011.Framework.Core;
using ScriptEase.Shell.Controls;



namespace ScriptEase.Shell.Commands
{
    internal class PublishWebResourceCommand : ICommand
    {
        private readonly ShellViewModel _shellViewModel;
        private readonly WebResourceStatusView _viewModel;



        public PublishWebResourceCommand( ShellViewModel shellViewModel, WebResourceStatusView viewModel)
        {
            _shellViewModel = shellViewModel;
            _viewModel = viewModel;
        }



        public void Execute(object parameter)
        {
            MappingModel[] selectedItems = _viewModel.SelectedItems.Where(
                item => item.SyncStatus == FileSyncStatus.PendingPublish).ToArray();

            foreach (MappingModel selectedItem in selectedItems)
            {
                _shellViewModel.Project.PublishWebResource(selectedItem);
            }
        }




        public bool CanExecute(object parameter)
        {
            return (_viewModel.SelectedItems.Any(item => item.SyncStatus == FileSyncStatus.PendingPublish));
        }



        public event EventHandler CanExecuteChanged;
    }
}